home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / scripts / general / diff.m < prev    next >
Text File  |  1996-11-20  |  2KB  |  69 lines

  1. ## Copyright (C) 1995, 1996  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## usage:  diff (x [, k])
  18. ##
  19. ## If x is a vector of length n, diff (x) is the vector of first
  20. ## differences x(2) - x(1), ..., x(n) - x(n-1).
  21. ##
  22. ## If x is a matrix, diff (x) is the matrix of column differences.
  23. ## diff (x, k), where k is a nonnegative integer, returns the k-th
  24. ## differences.
  25.  
  26. ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  27. ## Created: 2 February 1995
  28. ## Adapted-By: jwe
  29.  
  30. function x = diff (x, k)
  31.   
  32.   if (nargin == 1)
  33.     k = 1;
  34.   elseif (nargin == 2)
  35.     if (! (is_scalar (k) && k == round (k) && k >= 0))
  36.       error ("diff: k must be a nonnegative integer");
  37.     elseif (k == 0)
  38.       return;
  39.     endif
  40.   else
  41.     usage ("diff (x [, k]");
  42.   endif
  43.   
  44.   if (isstr (x))
  45.     error ("diff: symbolic differentiation not (yet) supported");
  46.   elseif (is_vector (x))
  47.     n = length (x);
  48.     if (n <= k)
  49.       x = [];
  50.     else
  51.       for i = 1 : k
  52.     x = x (2 : (n - i + 1)) - x (1 : (n - i));
  53.       endfor
  54.     endif
  55.   elseif (is_matrix (x))
  56.     n = rows (x);
  57.     if (n <= k)
  58.       x = [];
  59.     else
  60.       for i = 1 : k
  61.     x = x (2 : (n - i + 1), :) - x (1: (n - i), :);
  62.       endfor
  63.     endif
  64.   else
  65.     x = [];
  66.   endif
  67.  
  68. endfunction
  69.